<?php

namespace Core\Order\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

// use Illuminate\Notifications\Messages\NexmoMessage;
use App\Channels\MiniappTemplateChannel;

use Auth;
use Miniapp;

class OrderFreighted extends Notification
{
    use Queueable;

    public $order;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($order)
    {
        $this->order = $order;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        $channels = [];

        // 小程序模板通知
        $payment = $this->order->payment();

        if ($payment && $payment->isMiniappPay()) {
            array_push($channels, MiniappTemplateChannel::class);
        }


        return $channels;
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', 'https://laravel.com')
                    ->line('Thank you for using our application!');
    }

    /**
     * 获取通知的 Nexmo / 短信展示方式
     *
     * @param  mixed  $notifiable
     * @return NexmoMessage
     */
    public function toNexmo($notifiable)
    {
        return (new NexmoMessage)
        ->content('Your SMS message content');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }

    /**
     * 获取通知的模板消息
     *
     * @param  mixed  $notifiable
     * @return VoiceMessage
     */
    public function toMiniappTemplate($notifiable)
    {


        $payment = $this->order->payment();

        $miniappName = $payment->miniapp_name ?: 'default';
        $openid = $notifiable->routeNotificationForMiniappTemplate($miniappName);
        $order = $this->order;

        $formId = Miniapp::getFormId($order->user_id, $miniappName);

        if (!$formId) {
            return false;
        }

        if ($order->isOrderType('simple'))
            $page = 'pages/orderDetail/index?uuid=' . $order->uuid;
        else
            $page = 'pages/home/index';

        $freight = $order->single_freight;

        $message = [
            'touser' => $openid,
            'page' => $page,
            'form_id' => $formId,
            'data' => [
                'keyword1' => $order->number,
                'keyword2' => (String)$order->paid_at,
                'keyword3' => $order->generateTitle(),
                'keyword4' => $freight->freight_inc,
                'keyword5' => $freight->freight_number,
            ],
        ];

        return [
            $miniappName,
            'order_freighted',
            $message
        ];
    }
}
